package com.appkubes.fintech.admin.application.order.dao;

import cn.hutool.core.date.DateUtil;
import com.appkubes.fintech.admin.application.order.entity.vo.RentOrderVO;
import com.appkubes.fintech.admin.application.order.entity.vo.RentRepayOrderVO;
import com.appkubes.fintech.admin.application.user.entity.vo.UserBaseInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.text.SimpleDateFormat;
import java.util.Objects;

public class RentOrderSqlProvider {
    public String select(RentOrderVO request){
        return new SQL(){
            {

                SELECT(" ore.rent_order_key as orderKey, ore.is_sure as isSure , ore.rent_order_no as rentOrderNo, ore.goods_title AS goodsTitle,us.real_name AS realName,us.phone,ore.goods_amount AS goodsAmount, ore.buy_out_amount buyOutAmount,ore.fist_pay_amount fistPayAmount " +
                        " ,ore.status,ore.buy_status AS buyStatus,ore.rent_type AS rentType,ore.update_time AS updateTime ,ore.periods " +
                        " ,ore.rent_amount as rentAmount,ore.create_time as createTime,ore.pay_status as isPay,ore.contract_url as contractUrl,ore.user_key as userKey,us.ref_user_name as refUserName,us.ref_user_phone as refUserPhone" +
                        " ,us.ref_channel_name as refChannelName,mall.attr_json as attrJson,mao.mobile as shPhone, mao.name as shName,mao.address as shAddress " );
                FROM("  fintech_rent_order ore ,fintech_user_base_info us ,mall_order_goods mall ,mall_order mao   ");
                StringBuilder whereClause = new StringBuilder(" us.user_key=ore.user_key and mall.order_key=ore.order_key and mao.order_key=ore.order_key  ");

                if(request.getIsSure()!=null){
                    whereClause.append(" and ore.is_sure = ").append(request.getIsSure()).append(" ");
                }
                if(StringUtils.isNotBlank(request.getGoodsTitle())){
                    whereClause.append(" and ore.goods_title like '%").append(request.getGoodsTitle()).append("%' ");
                }
                if(StringUtils.isNotBlank(request.getRealName())){
                    whereClause.append(" and us.real_name like '%").append(request.getRealName()).append("%' ");
                }
                if(StringUtils.isNotBlank(request.getPhone())){
                    whereClause.append(" and us.phone = '").append(request.getPhone()).append("' ");
                }
                if(StringUtils.isNotBlank(request.getStatus())){
                    whereClause.append(" and ore.status = '").append(request.getStatus()).append("' ");
                }
                if(request.getStartDate()!=null){
                    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
                    whereClause.append(" and ore.create_time >= '").append(format.format(request.getStartDate())).append("' ");
                }
                if(request.getEndDate()!=null){
                    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
                    whereClause.append(" and ore.create_time <= '").append(format.format(request.getEndDate())).append("' ");
                }

                if (StringUtils.isNotBlank(request.getRefChannelName())) {
                    whereClause.append(" and us.ref_channel_name = '").append(request.getRefChannelName()).append("' ");
                }
                if (StringUtils.isNotBlank(request.getRefUserPhone())) {
                    whereClause.append(" and us.ref_user_phone = '").append(request.getRefUserPhone()).append("' ");
                }


                if(!"".equals(whereClause.toString())){
                    WHERE(whereClause.toString());
                }
                ORDER_BY(" ore.create_time desc");
            }
        }.toString();
    }

    public String selectBill(RentRepayOrderVO rentRepayOrderVO){
        return new SQL(){
            {

                SELECT(" bill.*,ore.rent_type" );
                FROM("  fintech_rent_bill_header bill ,fintech_rent_order ore    ");
                StringBuilder whereClause = new StringBuilder(" bill.order_key=ore.rent_order_key ");
                if(rentRepayOrderVO.getPeriodType()!=null){

                    whereClause.append(" and bill.period_type = '").append(rentRepayOrderVO.getPeriodType()).append("' ");
                }
                if(StringUtils.isNotBlank(rentRepayOrderVO.getUserName())){

                    whereClause.append(" and bill.user_name like '%").append(rentRepayOrderVO.getUserName()).append("%' ");
                }
                if(StringUtils.isNotBlank(rentRepayOrderVO.getStatus())){
                    whereClause.append(" and bill.status = '").append(rentRepayOrderVO.getStatus()).append("' ");

                }
                if(rentRepayOrderVO.getStartDate()!=null){

                    whereClause.append(" and bill.create_time >= '").append(DateUtil.format(rentRepayOrderVO.getStartDate(),"yyyy-MM-dd")).append("' ");
                }
                if(rentRepayOrderVO.getEndDate()!=null){
                    whereClause.append(" and bill.create_time <= '").append(DateUtil.format(rentRepayOrderVO.getEndDate(),"yyyy-MM-dd")).append("' ");

                }
                if(!"".equals(whereClause.toString())){
                    WHERE(whereClause.toString());
                }
            }
        }.toString();
    }

    public String selectRepayOrder(RentRepayOrderVO rentRepayOrderVO){
        return new SQL(){
            {

                SELECT("  oe.repay_key AS repayKey,oe.user_key AS userKey,oe.rent_order_key AS rentOrderKey,oe.period_type AS periodType," +
                        " oe.bill_key AS billKey , oe.bill_line_key AS billLineKey,oe.line_periods AS linePeriods, oe.pay_time AS payTime,oe.reapay_time AS reapayTime," +
                        " oe.user_name AS userName,oe.pay_trade_no AS payTradeNo, oe.pay_amt AS payAmt,oe.pay_type AS payType,oe.status AS STATUS,oe.create_time AS createTime," +
                        " oe.ret_repay_msg AS retRepayMsg,oe.ret_pay_msg AS retPayMsg  " );
                FROM("  fintech_rent_repay_order oe    ");
                StringBuilder whereClause = new StringBuilder(" 1=1 ");



                if(rentRepayOrderVO.getPeriodType()!=null){

                    whereClause.append(" and oe.period_type = '").append(rentRepayOrderVO.getPeriodType()).append("' ");
                }
                if(StringUtils.isNotBlank(rentRepayOrderVO.getUserName())){

                    whereClause.append(" and oe.user_name like '%").append(rentRepayOrderVO.getUserName()).append("%' ");
                }
                if(StringUtils.isNotBlank(rentRepayOrderVO.getStatus())){

                    whereClause.append(" and oe.status = '").append(rentRepayOrderVO.getStatus()).append("' ");
                }
                if(rentRepayOrderVO.getStartDate()!=null){


                    whereClause.append(" and oe.create_time >='").append(DateUtil.format(rentRepayOrderVO.getStartDate(),"yyyy-MM-dd")).append("' ");
                }
                if(rentRepayOrderVO.getEndDate()!=null){
                    whereClause.append(" and oe.create_time <='").append(DateUtil.format(rentRepayOrderVO.getEndDate(),"yyyy-MM-dd")).append("' ");
                }


                if(!"".equals(whereClause.toString())){
                    WHERE(whereClause.toString());
                }
                ORDER_BY("payTime desc ");
            }
        }.toString();
    }
    /**
     * @Author: XQC 2020/1/4/17:16
     * @params: 认证用户查询
     * @return:
     * @Description:
     **/
    public String selectAuth(UserBaseInfoVO request){
        return new SQL(){
            {
                SELECT(" base.real_name AS realName,base.phone ,cer.idcard_number as idcardNumber,base.is_phone_authentication phoneStatus\n" +
                        ",cer.portrait_auth_status AS portraitStatus " +
                        ",cer.idcard_auth_status AS idcardStatus,base.create_time AS createTime " );
                FROM(" fintech_user_base_info base LEFT JOIN fintech_user_certificate cer ON cer.user_key=base.user_key ");
                StringBuilder whereClause = new StringBuilder(" 1=1 ");

                if(StringUtils.isNotBlank(request.getRealName())){
                    whereClause.append(" and base.real_name like '%").append(request.getRealName()).append("%' ");
                }
                if(StringUtils.isNotBlank(request.getPhone())){
                    whereClause.append(" and base.phone = '").append(request.getPhone()).append("' ");
                }
                if(StringUtils.isNotBlank(request.getIdentityNum())){
                    whereClause.append(" and cer.idcard_number = '").append(request.getIdentityNum()).append("' ");
                }

                if(Objects.equals("1",request.getAuthType())){ //未做手机认证的
                    whereClause.append(" and ( base.is_phone_authentication is null or base.is_phone_authentication!=1 )");
                }
                if(Objects.equals("2",request.getAuthType())){ //未做手机认证的
                    whereClause.append(" and  base.is_phone_authentication =1 ");
                }
                if(Objects.equals("3",request.getAuthType())){ //未做手机认证的
                    whereClause.append(" and  base.idcard_auth_status = 1 ");
                }
                if(Objects.equals("4",request.getAuthType())){ //未做手机认证的
                    whereClause.append(" and  base.portrait_auth_status =1 ");
                }

                if(request.getStartDate()!=null){
                    whereClause.append(" and base.create_time >= '").append(DateUtil.format(request.getStartDate(),"yyyy-MM-dd HH:mm:ss")).append("' ");
                }
                if(request.getEndDate()!=null){
                    whereClause.append(" and base.create_time <= '").append(DateUtil.format(request.getEndDate(),"yyyy-MM-dd HH:mm:ss")).append("' ");
                }
                if(request.getPhoneStatus()!=null){
                    whereClause.append(" and cer.phone_auth_status = '").append(request.getPhoneStatus()).append("' ");
                }
                if(request.getIdCardStatus()!=null){
                    whereClause.append(" and cer.idcard_auth_status = '").append(request.getIdCardStatus()).append("' ");
                }
                if(request.getPortraitStatus()!=null){
                    whereClause.append(" and hea.portrait_auth_status = '").append(request.getPortraitStatus()).append("' ");
                }
                if(!"".equals(whereClause.toString())){
                    WHERE(whereClause.toString());
                }

                ORDER_BY( " base.create_time desc" );
            }
        }.toString();
    }

}
